package com.leon.common.entity;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.leon.common.common.DateUtil;
import lombok.Data;

/**
 * @description: 响应实体
 * @author: WT
 * @date: 2024/3/31 18:38
 * @Version 1.0
 **/
@Data
public class ResponseVo<T> {
    /**
     * 封装head信息
     */
    @JSONField(name="header")
    private ResponseHead header = new ResponseHead();
    /**
     * 返回body信息
     */
    @JSONField(name="body")
    private Object body;
    public ResponseVo error(RequestBodyVo.RequestHead requestHead, String errors){
        if(requestHead == null){
            // ResponseVo responseVo = new ResponseVo();
            this.setError("9999", errors);
            return this;
            //  return responseVo;
        }
        header.seriaNo =requestHead.getSeriaNo();
        header.transCode = requestHead.getTransCode();
        header.transDate = DateUtil.getCur10Date();
        header.transTime = DateUtil.getCur8Time();
        header.sysCode = requestHead.getSysCode();
        header.totalRowNum = 0L;
        header.retCode = "9999";
        header.retMsg = errors;
        return this;
    }
    public void setError(String retCode, String retMsg){
        header.retCode = retCode;
        header.retMsg = retMsg;
    }

    /**
     * 成功
     * @param requestHead
     * @return
     */
    public ResponseVo success(RequestBodyVo.RequestHead requestHead){
        header.seriaNo =requestHead.getSeriaNo();
        header.transCode = requestHead.getTransCode();
        header.transDate = DateUtil.getCur10Date();
        header.transTime = DateUtil.getCur8Time();
        header.sysCode = requestHead.getSysCode();
        header.retCode = "0000";
        header.retMsg = "成功";
        return this;
    }
    /**
     * 成功
     * @param requestHead
     * @return
     */
    public ResponseVo success(RequestBodyVo.RequestHead requestHead, Object value){
        header.seriaNo =requestHead.getSeriaNo();
        header.transCode = requestHead.getTransCode();
        header.transDate = DateUtil.getCur10Date();
        header.transTime = DateUtil.getCur8Time();
        header.sysCode = requestHead.getSysCode();
        header.retCode = "0000";
        header.retMsg = "成功";
        this.body = value;
        return this;
    }
    public void setValue(RequestBodyVo.RequestHead requestHead, Object value, Long total) {
        header.seriaNo =requestHead.getSeriaNo();
        header.transCode = requestHead.getTransCode();
        header.transDate = DateUtil.getCur10Date();
        header.transTime = DateUtil.getCur8Time();
        header.sysCode = requestHead.getSysCode();
        header.totalRowNum = total;
        header.retCode = "0000";
        header.retMsg = "成功";
        body =  value;
    }
    public void setValue(RequestBodyVo.RequestHead requestHead, String key , Object value, Long total) {
        header.seriaNo =requestHead.getSeriaNo();
        header.transCode = requestHead.getTransCode();
        header.transDate = DateUtil.getCur10Date();
        header.transTime = DateUtil.getCur8Time();
        header.sysCode = requestHead.getSysCode();
        header.totalRowNum = total;
        header.retCode = "0000";
        header.retMsg = "成功";
        JSONObject json = new JSONObject( );
        json.put(key, value);
        body = json;
    }

    @Data
    class ResponseHead {
        /**
         * 流水号
         */
        String seriaNo;
        /**
         * 接口编码
         */
        String transCode;
        /**
         * 请求日期
         */
        String transDate;
        /**
         * 请求时间
         */
        String transTime;
        /**
         * 请求系统
         */
        String sysCode;
        /**
         * 返回标识码
         */
        String retCode;
        /**
         * 返回信息
         */
        String retMsg;
        /**
         * 记录数
         */
        Long totalRowNum;
        /**
         * pageNum
         */
        Long pageNum;

    }
}